package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.manager.DepartmentManager;
import com.xbongbong.paas.pojo.dto.DepartmentFlatListDTO;
import com.xbongbong.paas.pojo.dto.DepartmentGetDTO;
import com.xbongbong.paas.pojo.dto.DepartmentListDTO;
import com.xbongbong.paas.pojo.dto.DepartmentRefreshDTO;
import com.xbongbong.paas.pojo.dto.DepartmentRefreshStatusDTO;
import com.xbongbong.paas.pojo.dto.DepartmentRefreshTimesDTO;
import com.xbongbong.paas.pojo.dto.DepartmentSimpleListDTO;
import com.xbongbong.paas.pojo.vo.DepartmentFlatListVO;
import com.xbongbong.paas.pojo.vo.DepartmentGetVO;
import com.xbongbong.paas.pojo.vo.DepartmentListVO;
import com.xbongbong.paas.pojo.vo.DepartmentRefreshStatusVO;
import com.xbongbong.paas.pojo.vo.DepartmentRefreshTimesVO;
import com.xbongbong.paas.pojo.vo.DepartmentRefreshVO;
import com.xbongbong.paas.pojo.vo.DepartmentSimpleListVO;
import com.xbongbong.paas.service.DepartmentService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.department.pojo.dto.DepartmentListRelatedDTO;
import com.xbongbong.pro.department.pojo.dto.DepartmentUserListDTO;
import com.xbongbong.pro.department.pojo.vo.DepartmentListRelatedVO;
import com.xbongbong.pro.department.pojo.vo.DepartmentUserListVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author kaka
 * @time 2018-9-5 15:12
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/department")
public class DepartmentController {

    @Resource
    private DepartmentManager departmentManager;
    @Resource
    private DepartmentService departmentService;

    /**
     * 同步组织架构
     * 调用 dingtalk 项目相应接口完成逻辑
     * @param departmentRefreshDTO 关联业务DTO
     */
    @RequestMapping(value = "/refresh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String refresh(@RequestBody @Valid DepartmentRefreshDTO departmentRefreshDTO, BindingResult br) throws Exception {
        XbbResponse<DepartmentRefreshVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DepartmentRefreshVO refreshVO = departmentManager.refresh(departmentRefreshDTO);
                response = new XbbResponse<>(refreshVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取当天同步组织架构剩余次数
     * @param refreshTimesDTO 关联业务DTO
     */
    @RequestMapping(value = "/getRefreshTimes", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getRefreshTimes(@RequestBody @Valid DepartmentRefreshTimesDTO refreshTimesDTO, BindingResult br) throws Exception {
        XbbResponse<DepartmentRefreshTimesVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DepartmentRefreshTimesVO refreshTimesVO = departmentManager.getFreshTimes(refreshTimesDTO);
                response = new XbbResponse<>(refreshTimesVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取当前同步组织架构状态
     * @param refreshStatusDTO 关联业务DTO
     */
    @RequestMapping(value = "/refreshStatus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String refreshStatus(@RequestBody @Valid DepartmentRefreshStatusDTO refreshStatusDTO, BindingResult br) throws Exception {
        XbbResponse<DepartmentRefreshStatusVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DepartmentRefreshStatusVO refreshStatusVO = departmentManager.getFreshStatus(refreshStatusDTO);
                response = new XbbResponse<>(refreshStatusVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 部门列表接口
     * @param departmentListDTO 关联业务DTO
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid DepartmentListDTO departmentListDTO, BindingResult br) throws Exception {
        XbbResponse<DepartmentListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DepartmentListVO departmentListVO = departmentService.list(departmentListDTO);
                response = new XbbResponse<>(departmentListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取平铺的部门列表，支持普通js调用or接口调用，支持分页
     * 部门平铺--无层级结构
     * @param departmentListDTO 关联业务DTO
     */
    @RequestMapping(value = "/flatList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid DepartmentFlatListDTO departmentListDTO, BindingResult br) throws Exception {
        XbbResponse<DepartmentFlatListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            //请求参数后处理，比如trim
            departmentListDTO.postProcess();
            try {
                DepartmentFlatListVO departmentFlatListVO = departmentService.flatList(departmentListDTO);
                response = new XbbResponse<>(departmentFlatListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 根据部门id列表获取部门数据列表
     * @param departmentGetDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid DepartmentGetDTO departmentGetDTO, BindingResult br) throws Exception {
        XbbResponse<DepartmentGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DepartmentGetVO departmentGetVO = departmentService.getByDepartmentIds(departmentGetDTO);
                response = new XbbResponse<>(departmentGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 部门列表接口,简单数据格式
     * [{
     *  "value" : 1,
     *  "label" : "一级部门",
     *  "children" : [{
     *      "value" : 11,
     *      "label" : "二级部门1"
     *  },{
     *      "value" : 12,
     *      "label" : "二级部门2"
     *  }]
     * }]
     * @param simpleListDTO 关联业务DTO
     */
    @RequestMapping(value = "/simpleList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String simpleList(@RequestBody @Valid DepartmentSimpleListDTO simpleListDTO, BindingResult br) throws Exception {
        XbbResponse<DepartmentSimpleListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DepartmentSimpleListVO simpleListVO = departmentService.simpleList(simpleListDTO);
                response = new XbbResponse<>(simpleListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response);
    }

    /**
     * 根据用户id获取部门数据列表
     * @param departmentListRelatedDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/relatedList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String relatedList(@RequestBody @Valid DepartmentListRelatedDTO departmentListRelatedDTO, BindingResult br) throws Exception {
        XbbResponse<DepartmentListRelatedVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DepartmentListRelatedVO simpleListVO = departmentService.relatedList(departmentListRelatedDTO);
                response = new XbbResponse<>(simpleListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response);
    }

    /**
     * 获取部门和用户列表
     * @param departmentUserListDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/userList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String userList(@RequestBody @Valid DepartmentUserListDTO departmentUserListDTO, BindingResult br) throws Exception {
        XbbResponse<DepartmentUserListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DepartmentUserListVO simpleListVO = departmentService.departmentUserList(departmentUserListDTO);
                response = new XbbResponse<>(simpleListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response);
    }
}
